﻿using AjaxControlToolkit;
using BLL;
using BLL.Pictures;
using DTO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imageShow.ImageUrl = Picture.GetPictureUrl(9, 100, false, null);
    }

    protected void AsyncFileUploadComplete(object sender, AsyncFileUploadEventArgs e)
    {
        string contentType = "";
        string fileName = AsyncFileUpload1.PostedFile.FileName;
        string seofileName = Helpers.GetSeName(Path.GetFileNameWithoutExtension(fileName), false, false);
        Stream stream = AsyncFileUpload1.PostedFile.InputStream;
        byte[] pictureBinary = new byte[stream.Length];
        stream.Read(pictureBinary, 0, pictureBinary.Length);

        string fileExtension = Path.GetExtension(fileName);
        if (!String.IsNullOrEmpty(fileExtension))
            fileExtension = fileExtension.ToLowerInvariant();

        if (String.IsNullOrEmpty(contentType))
        {
            switch (fileExtension)
            {
                case ".bmp":
                    contentType = "image/bmp";
                    break;
                case ".gif":
                    contentType = "image/gif";
                    break;
                case ".jpeg":
                case ".jpg":
                case ".jpe":
                case ".jfif":
                case ".pjpeg":
                case ".pjp":
                    contentType = "image/jpeg";
                    break;
                case ".png":
                    contentType = "image/png";
                    break;
                case ".tiff":
                case ".tif":
                    contentType = "image/tiff";
                    break;
                default:
                    break;
            }
        }

        int ret = Picture.InsertPicture(pictureBinary, contentType, seofileName, true);
    }

    protected void FileUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
        string contentType = "";
        string fileName = e.FileName;        
        string seofileName = Helpers.GetSeName(e.FileName, false, false);
        byte[] pictureBinary = e.GetContents();

        string fileExtension = Path.GetExtension(fileName);
        if (!String.IsNullOrEmpty(fileExtension))
            fileExtension = fileExtension.ToLowerInvariant();

        if (String.IsNullOrEmpty(contentType))
        {
            switch (fileExtension)
            {
                case ".bmp":
                    contentType = "image/bmp";
                    break;
                case ".gif":
                    contentType = "image/gif";
                    break;
                case ".jpeg":
                case ".jpg":
                case ".jpe":
                case ".jfif":
                case ".pjpeg":
                case ".pjp":
                    contentType = "image/jpeg";
                    break;
                case ".png":
                    contentType = "image/png";
                    break;
                case ".tiff":
                case ".tif":
                    contentType = "image/tiff";
                    break;
                default:
                    break;
            }
        }
        int ret = Picture.InsertPicture(pictureBinary, contentType, seofileName, true);
    }
}
